colorchooser: factor out a private method to get the checkboard pattern
authorCosimo Cecchi <cosimoc@gnome.org>
Tue, 14 Feb 2012 17:16:56 +0000 (12:16 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 14 Feb 2012 21:37:04 +0000 (16:37 -0500)
And use it in the color widgets.

gtk/gtkcolorbutton.c
gtk/gtkcolorchooser.c
gtk/gtkcolorchooserprivate.h
gtk/gtkcolorscale.c
gtk/gtkcolorswatch.c

index c046cc18888496d0faab5c7acd73a72874c23469..d9aeabc642eb8066442c294b2577c040dfbcab87 100644 (file)
@@ -36,6 +36,7 @@
 #include "gtkbutton.h"
 #include "gtkmain.h"
 #include "gtkcolorchooser.h"
+#include "gtkcolorchooserprivate.h"
 #include "gtkcolorchooserdialog.h"
 #include "gtkdnd.h"
 #include "gtkdrawingarea.h"
@@ -271,29 +272,6 @@ gtk_color_button_has_alpha (GtkColorButton *button)
   return button->priv->use_alpha && button->priv->rgba.alpha < 1;
 }
 
-static cairo_pattern_t *
-gtk_color_button_get_checkered (void)
-{
-  /* need to respect pixman's stride being a multiple of 4 */
-  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
-                                   0x00, 0xFF, 0x00, 0x00 };
-  static cairo_surface_t *checkered = NULL;
-  cairo_pattern_t *pattern;
-
-  if (checkered == NULL)
-    {
-      checkered = cairo_image_surface_create_for_data (data,
-                                                       CAIRO_FORMAT_A8,
-                                                       2, 2, 4);
-    }
-
-  pattern = cairo_pattern_create_for_surface (checkered);
-  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
-  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
-
-  return pattern;
-}
-
 /* Handle exposure events for the color picker's drawing area */
 static gint
 gtk_color_button_draw_cb (GtkWidget *widget,
@@ -311,7 +289,7 @@ gtk_color_button_draw_cb (GtkWidget *widget,
       cairo_set_source_rgb (cr, CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT);
       cairo_scale (cr, CHECK_SIZE, CHECK_SIZE);
 
-      checkered = gtk_color_button_get_checkered ();
+      checkered = _gtk_color_chooser_get_checkered_pattern ();
       cairo_mask (cr, checkered);
       cairo_pattern_destroy (checkered);
 
@@ -336,7 +314,7 @@ gtk_color_button_draw_cb (GtkWidget *widget,
       gtk_style_context_get_background_color (context, GTK_STATE_FLAG_INSENSITIVE, &color);
 
       gdk_cairo_set_source_rgba (cr, &color);
-      checkered = gtk_color_button_get_checkered ();
+      checkered = _gtk_color_chooser_get_checkered_pattern ();
       cairo_mask (cr, checkered);
       cairo_pattern_destroy (checkered);
     }
index 075331ba3b8f5f1b67fb3aa778a1060051346384..a54127b614ec18e026cb8768678dd19f3ba84326 100644 (file)
@@ -235,3 +235,24 @@ gtk_color_chooser_add_palette (GtkColorChooser *chooser,
   if (GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette)
     GTK_COLOR_CHOOSER_GET_IFACE (chooser)->add_palette (chooser, horizontal, colors_per_line, n_colors, colors);
 }
+
+cairo_pattern_t *
+_gtk_color_chooser_get_checkered_pattern (void)
+{
+  /* need to respect pixman's stride being a multiple of 4 */
+  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
+                                   0x00, 0xFF, 0x00, 0x00 };
+  static cairo_surface_t *checkered = NULL;
+  cairo_pattern_t *pattern;
+
+  if (checkered == NULL)
+    checkered = cairo_image_surface_create_for_data (data,
+                                                     CAIRO_FORMAT_A8,
+                                                     2, 2, 4);
+
+  pattern = cairo_pattern_create_for_surface (checkered);
+  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
+  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
+
+  return pattern;
+}
index 0a9f4f5e6bd86074e7670fb809c3fbcd7aaee5c3..4109901ca1ae5df58c36e4ffaac5554174f77688 100644 (file)
@@ -27,6 +27,8 @@ G_BEGIN_DECLS
 void _gtk_color_chooser_color_activated (GtkColorChooser *chooser,
                                          const GdkRGBA   *color);
 
+cairo_pattern_t * _gtk_color_chooser_get_checkered_pattern (void);
+
 G_END_DECLS
 
 #endif /* ! __GTK_COLOR_CHOOSER_PRIVATE_H__ */
index 7070281b8711065c19aa999f41c079b098189197..7cfa5a03bced928fb80c1774f953ed36bc34b984 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "gtkcolorscaleprivate.h"
 
+#include "gtkcolorchooserprivate.h"
 #include "gtkcolorutils.h"
 #include "gtkorientable.h"
 #include "gtkstylecontext.h"
@@ -44,27 +45,6 @@ enum
 
 G_DEFINE_TYPE (GtkColorScale, gtk_color_scale, GTK_TYPE_SCALE)
 
-static cairo_pattern_t *
-get_checkered_pattern (void)
-{
-  /* need to respect pixman's stride being a multiple of 4 */
-  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
-                                   0x00, 0xFF, 0x00, 0x00 };
-  static cairo_surface_t *checkered = NULL;
-  cairo_pattern_t *pattern;
-
-  if (checkered == NULL)
-    checkered = cairo_image_surface_create_for_data (data,
-                                                     CAIRO_FORMAT_A8,
-                                                     2, 2, 4);
-
-  pattern = cairo_pattern_create_for_surface (checkered);
-  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
-  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
-
-  return pattern;
-}
-
 static void
 create_surface (GtkColorScale *scale)
 {
@@ -152,7 +132,7 @@ create_surface (GtkColorScale *scale)
       cairo_paint (cr);
       cairo_set_source_rgb (cr, 0.66, 0.66, 0.66);
 
-      pattern = get_checkered_pattern ();
+      pattern = _gtk_color_chooser_get_checkered_pattern ();
       cairo_matrix_init_scale (&matrix, 0.125, 0.125);
       cairo_pattern_set_matrix (pattern, &matrix);
       cairo_mask (cr, pattern);
index 10ec66ec34a25086afd42652c1836caaf985ea8d..8e9c38525e6442246b8f5a4d5ba87ffc2e386ae9 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "gtkcolorswatchprivate.h"
 
+#include "gtkcolorchooserprivate.h"
 #include "gtkroundedboxprivate.h"
 #include "gtkthemingbackgroundprivate.h"
 #include "gtkdnd.h"
@@ -78,27 +79,6 @@ gtk_color_swatch_init (GtkColorSwatch *swatch)
 
 #define INTENSITY(r, g, b) ((r) * 0.30 + (g) * 0.59 + (b) * 0.11)
 
-static cairo_pattern_t *
-get_checkered_pattern (void)
-{
-  /* need to respect pixman's stride being a multiple of 4 */
-  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
-                                   0x00, 0xFF, 0x00, 0x00 };
-  static cairo_surface_t *checkered = NULL;
-  cairo_pattern_t *pattern;
-
-  if (checkered == NULL)
-    checkered = cairo_image_surface_create_for_data (data,
-                                                     CAIRO_FORMAT_A8,
-                                                     2, 2, 4);
-
-  pattern = cairo_pattern_create_for_surface (checkered);
-  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
-  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
-
-  return pattern;
-}
-
 static gboolean
 swatch_draw (GtkWidget *widget,
              cairo_t   *cr)
@@ -141,7 +121,7 @@ swatch_draw (GtkWidget *widget,
           cairo_set_source_rgb (cr, 0.33, 0.33, 0.33);
           cairo_fill_preserve (cr);
 
-          pattern = get_checkered_pattern ();
+          pattern = _gtk_color_chooser_get_checkered_pattern ();
           cairo_matrix_init_scale (&matrix, 0.125, 0.125);
           cairo_pattern_set_matrix (pattern, &matrix);